package objects.alignments;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import objects.Subset;
import objects.ValidPosition;

import Control.Zoomable;

public class SpliceAlignment extends SingleAlignment {

	private int end;
	
	public int getEnd() {
		return end;
	}
	
	public void setEnd(int end) {
		this.end = end;
	}
	
	public SpliceAlignment(int start, Subset sub, String cigarString) {
		super(start, 1, sub, cigarString);
		setStart(this.getValidPositions().get(0).end);
		if(this.getValidPositions().size() > 1)
			setEnd(this.getValidPositions().get(1).start);
		else
			setEnd(getStart());
	}

	@Override
	public void draw(Graphics graphic, int x, int y) {
		// Since it's a splice site alignment, the valid positions should be
		// more than 1.
		if (this.getValidPositions().size() <= 1)
			System.out.println("SpliceAlignment size is off: "
					+ this.getValidPositions().size());

		Color color = this.getParent().getTreatmentParent().getColor();
		Graphics2D g2d = (Graphics2D) graphic;
		Zoomable z = Zoomable.getInstance();

		int oldOffset = 0;
		for (ValidPosition vp : getValidPositions()) {
			g2d.setColor(color);
			vp.x = x + z.calcDist(vp.start);
			vp.y = y;
			vp.width = z.calcDist(vp.getDistance());
			vp.height = 4;
			g2d.fill(vp);
			g2d.setColor(new Color(100, 100, 100));
			g2d.draw(vp);
			if (oldOffset != 0) {
				g2d.drawLine(oldOffset, y + 2, x + z.calcDist(vp.start), y + 2);
			}
			oldOffset = x + z.calcDist(vp.end);
		}
	}

	@Override
	public String toString() {
		return "Splice Alignment";
	}
	
	@Override
	public boolean equals(Alignment splice) {
		if (!(splice instanceof SpliceAlignment))
			return false;

		// Since they are both spliced alignments, they should have two alignments.
		return (getStart() == splice.getStart() && getEnd() == ((SpliceAlignment)splice).getEnd());
	}
	
	@Override
	public int compareTo(Alignment alignment) {
		// It's not a Splice Alignment, so check for it's alignment
		if (!(alignment instanceof SpliceAlignment)) 
		{
			return getStart() - alignment.getStart();
		}
		
		// Since they are both spliced alignments, they should have two alignments.
		int otherStart = ((SpliceAlignment)alignment).getStart();
		int otherEnd   = ((SpliceAlignment)alignment).getEnd();
		
		if (getStart() == otherStart)
			return (getEnd() - otherEnd);
		return getStart() - otherStart;
	}
	
	public int compareTo(SpliceAlignment spliced) 
	{
		if (getStart() == spliced.getStart())
			return (getEnd() - spliced.getEnd());
		return getStart() - spliced.getStart();
	}
}
